<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Org\Util;
use Think\Model;

require_once 'ThinkPHP/Library/Org/Util/httpful/bootstrap.php';

/**
 * 后台物流人员用户控制器
 * @author James 2015-10-14
 */
class LogisticsUserController extends AdminController {
    
    /**
     * 快递用户管理首页
     * @author James 2015-10-14
     */
    public function index(){
        $name=I('name');
        $phone=I('phone');
        $where = "status >= 1 and delflag = 0";
        if($name){
            $where = $where." and name like '%".(string)$name."%'";
        }
        if($phone){
            $where = $where." and phone like '%".(string)$phone."%'";
        }
        
        $AeQuery = new Util\AEQuery("api","lg_courier");
        $AeObjectArray = $AeQuery->find($where);
        $lguserarr = array();
        for($i=0;$i<count($AeObjectArray);$i++){
            $id = $AeObjectArray[$i]->get("id");
            $role = $AeObjectArray[$i]->get("role");
            $name = $AeObjectArray[$i]->get("name");
            $phone = $AeObjectArray[$i]->get("phone");
            $area = $AeObjectArray[$i]->get("area");
            $createAt = $AeObjectArray[$i]->get("createAt");
            $status = $AeObjectArray[$i]->get("status");
            $lguserarr[$i]['id'] = $id;
            $lguserarr[$i]['role'] = $role;
            $lguserarr[$i]['name'] = $name;
            $lguserarr[$i]['phone'] = $phone;
            $lguserarr[$i]['area'] = $area;
            $lguserarr[$i]['createAt'] = $createAt;
            $lguserarr[$i]['status'] = $status;
        }
        
        $this->assign('_list', $lguserarr);
        $this->meta_title = '用户信息';
        $this->display();
    }
    
    /**
     * 派件增长率指标
     * @author James 2015-10-14
     */
    public function paramindex(){
        $map['delflag']  =   0;
        $list = $this->lists('logistics_param', $map,'createAt desc');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    
    /**
     * 新增物流人员
     * @author James 2015-10-14
     */
    public function add($username = '',$phone = '', $password = '', $repassword = '',$urole=""){
        if(IS_POST){
            /* 检测密码 */
            if($password != $repassword){
                $this->error('密码和重复密码不一致！');
            }
            
            $AeQuery = new Util\AEQuery("api","lg_courier");
            $AeObjectArray = $AeQuery->find("phone = ".$phone.' and delflag = 0 ');
            if(count($AeObjectArray) >= 1){
                $this->error('该手机号已注册过账号！');
            }
            
            
            $area_arry = I('area');
            for($i=0;$i<count($area_arry);$i++){
                $areaNames .= '"'.split("-", $area_arry[$i])[1].'",';
                $areaIds .= '"'.split("-", $area_arry[$i])[0].'",';
            }
            
            if($areaNames){
                $areaNames = substr($areaNames,0,strlen($areaNames)-1);
            }else{
                $areaNames = "";
            }
            
            if($areaIds){
                $areaIds = substr($areaIds,0,strlen($areaIds)-1);
            }else{
                $areaIds = "";
            }
            $AeObject = new Util\AEObject("api","lg_courier");
            $AeObject->set("role",$urole);
            $AeObject->set("password",$password);
            $AeObject->set("phone",$phone);
            $AeObject->set("name",$username);
            $AeObject->set("area",$areaNames);
            $AeObject->set("areaIds",$areaIds);
            $AeObject->set("status",1);
            $AeObject->set("createAt",NOW_TIME);
            $AeObject->set("updateAt",NOW_TIME);
            $AeObject->save();
            $this->success('用户添加成功！', U('index'));
        } else {
            $AeQuery = new Util\AEQuery("ec","gr_area");
            $AeObjectArray = $AeQuery->find("status=1");
            $arealist = array();
            for($i=0;$i<count($AeObjectArray);$i++){
                $id = $AeObjectArray[$i]->get("id");
                $areaName = $AeObjectArray[$i]->get("areaName");
                $arealist[$i]['id'] = $id;
                $arealist[$i]['areaName'] = $areaName;
            }
            $data['areaList'] = $arealist;
            $this->data = $data;
        
            $this->meta_title = '新增用户';
            $this->display();
        }
    }
    
    // 新增派件增长率指标
    public function paramadd($month="",$A1="",$A2="",$A3="",$B1="",$B2=""){
        if(IS_POST){
            $year = date('Y');
            $month=$year.$month;
            $m = M('logistics_param')->where(array('month'=>$month,'delflag'=>0))->select();
            if(count($m)!=0){
                $this->error("该月份已有指标，请编辑该月份指标或删除后再新增！");
            }
            $regulation = array('month' => $month, 'A1' => $A1, 'A2' => $A2, 'A3' => $A3,'B1' => $B1,'B2' => $B2, 'createAt' => NOW_TIME,'updateAt'=>NOW_TIME);
            $id = M('logistics_param')->add($regulation);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('paramindex'));
            }
        }else{
            $this->display();
        }
    }
    
    /**
     * 修改物流人员
     * @author James 2015-10-14
     */
    public function edit($name = "", $phone = "",$role="") {
        $id = I('id');
        if (IS_POST) {
            
            $area_arry = I('area');
            for ($i = 0; $i < count($area_arry); $i++) {
                $areaNames .= '"'.split("-", $area_arry[$i])[1].'",';
                $areaIds .= '"'.split("-", $area_arry[$i])[0].'",';
            }
            
            if($areaNames){
                $areaNames = substr($areaNames,0,strlen($areaNames)-1);
            }else{
                $areaNames = "";
            }
            
            if($areaIds){
                $areaIds = substr($areaIds,0,strlen($areaIds)-1);
            }else{
                $areaIds = "";
            }
            $AeQuery= new Util\AEQuery("api","lg_courier");
            $AeObject = $AeQuery->get($id);
            $AeObject->set("role",$role);
            $AeObject->set("phone",$phone);
            $AeObject->set("name",$name);
            $AeObject->set("area",$areaNames);
            $AeObject->set("areaIds",$areaIds);
            $AeObject->set("status",1);
            $AeObject->set("createAt",NOW_TIME);
            $AeObject->set("updateAt",NOW_TIME);
            $AeObject->save();
            $this->success('用户修改成功！', U('index'));
        } else {
            $AeQuery = new Util\AEQuery("api","lg_courier");
            $AeObject = $AeQuery->get($id);
            $data['id'] = $AeObject->get("id");
            $data['name'] = $AeObject->get("name");
            $data['phone'] = $AeObject->get("phone");
            $data['areaIds'] = $AeObject->get("areaIds");
            $areaids = $AeObject->get("areaIds");
            $areaids = explode(",", $areaids);
            for($i=0;$i<count($areaids);$i++){
                $areaids[$i] = intval(substr($areaids[$i], 1,  strlen($areaids[$i])-1));
            }

            $AeQuery1 = new Util\AEQuery("ec","gr_area");
            $AeObjectArray1 = $AeQuery1->find("status=1");
            $arealist = array();
            for($i=0;$i<count($AeObjectArray1);$i++){
                $id = $AeObjectArray1[$i]->get("id");
                $areaName = $AeObjectArray1[$i]->get("areaName");
                $arealist[$i]['id'] = $id;
                $arealist[$i]['areaName'] = $areaName;
            }
            $data['areaList'] = $arealist;
            $this->assign("role",$AeObject->get("role"));
            $this->areaids = $areaids;
            $this->areaid = $data['areaIds'];
            $this->data = $data;

            $this->display();
        }
    }
    
    // 派件增长率指标编辑
    public function paramedit($A1="",$A2="",$A3="",$B1="",$B2="") {
        $id = I('id');
        if (IS_POST) {
            $regulation = array('A1' => $A1, 'A2' => $A2, 'A3' => $A3,'B1' => $B1,'B2' => $B2,'updateAt'=>NOW_TIME);
            if (!M('logistics_param')->where(array('id' => $id))->save($regulation)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('paramindex'));
            }
        } else {
            $data = M('logistics_param')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->assign('id',$id);
            $this->display();
        }
    }
    
    /**
     * 删除物流人员
     * @author James 2015-10-14
     */
    public function del($id = 0) {
        if ($id) {
            
            $AeQuery = new Util\AEQuery("api","lg_courier");
            $AeObject = $AeQuery->get($id);
            if($AeObject->get('delflag')==0){
                $AeObject->set('delflag',1);
                $AeObject->save();
                $this->success('删除成功！', U('index'));
            }
            $this->error("删除失败！");
        }
    }
    
    // 派件增长率指标删除
    public function paramdel($id = 0) {
        if ($id) {
            $regulations = M("logistics_param");
            $data = array('delflag'=>1);
            $result = $regulations-> where(array('id'=>$id))->setField($data);
            if($result){
                $this->success('删除成功！', U('paramindex'));
            }else{
                $this->error("删除失败！");
            }
        }
    }
    
    /**
     * 会员状态修改
     * @author 朱亚杰 <zhuyajie@topthink.net>
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                dump('forbiduser');
                $u = M('logisticsuser')->where(array('id'=>$id))->select();
                $avqure = new \Org\Util\AVQuery("Courier");
                $where = array('jobnumber' => $u[0]['jobnumber']);
                $avq = $avqure->getFind($where);
                $oid = $avq->objectId;
                
                $av1 = $avqure->get($oid);
                $av1->set("status", "0");
                $id = $av1->save();
                
                $this->forbid('logisticsuser', $map );
                break;
            case 'resumeuser':
                dump('resumeuser');
                $u = M('logisticsuser')->where(array('id'=>$id))->select();
                $avqure = new \Org\Util\AVQuery("Courier");
                $where = array('jobnumber' => $u[0]['jobnumber']);
                $avq = $avqure->getFind($where);
                $oid = $avq->objectId;
                
                $av1 = $avqure->get($oid);
                $av1->set("status", "1");
                $id = $av1->save();
                
                $this->resume('logisticsuser', $map );
                break;
            case 'deleteuser':
                $this->delete('logisticsuser', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
    
    public function apppush($pushuser="",$pushtags="",$pushalias="",$pushcontent=""){
        if(IS_POST){
            if ($pushuser==1){
                $AEHttp=new Util\AEHttp("api");
                $param['content']=$pushcontent;
                $result=$AEHttp->postapi(".push.boardcast",$param);
                if($result['sendno']){
                    $this->success('推送成功！', U('apppush'));
                }else{
                    $this->error('推送失败！', U('apppush'));
                }
            }
            if ($pushuser==2){
                $AEHttp=new Util\AEHttp("api");
                $param['tags']=$pushtags;
                $param['content']=$pushcontent;
                $result=$AEHttp->postapi(".push.notify",$param);
                if($result['sendno']){
                    $this->success('推送成功！', U('apppush'));
                }else{
                    $this->error('推送失败！', U('apppush'));
                }
            }
            if ($pushuser==3){
                $AEHttp=new Util\AEHttp("api");
                $param['alias']=$pushalias;
                $param['content']=$pushcontent;
                $result=$AEHttp->postapi(".push.notify",$param);
                if($result['sendno']){
                    $this->success('推送成功！', U('apppush'));
                }else{
                    $this->error('推送失败！', U('apppush'));
                }
            }
        }
        $this->display();
    }
}
